<?php
/*
* Class teia\webdev\php\core\Catalog
* Core catalog
*
* @author Bernardo Quibiana
* @date created: 20111122
*
* @date modified: 20111122
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

require_once 'DisplayableItem.inc';
require_once 'IPaginationDataSource.inc';

class Catalog extends DisplayableItem implements IPaginationDataSource {
	const MIN_NUM_ROWS = 2;
	const MIN_NUM_COLS = 1;
	
	protected $items;
	protected $row_size;
	protected $col_size;
	protected $filter;
	
	/**
	 * Enter description here ...
	 * @param string $id
	 * @param string $name
	 * @param array $var_array
	 * @param array $items
	 * @param int $rows
	 * @param int $cols
	 * @param array $filter
	 */
	public function __construct($id, $name, $var_array, $items, $rows, $cols, $filter=NULL) {
		parent::__construct($id, $name, $var_array);
	
		$this->row_size = $rows > self::MIN_NUM_ROWS ? $rows : self::MIN_NUM_ROWS;
		$this->col_size = $cols > self::MIN_NUM_COLS ? $cols : self::MIN_NUM_COLS;
		$this->filter = isset($filter) ? $filter : null;
		$pass = true; //FunctionPool::check_elements_class($items, 'teia\DisplayableItem');
		if ($pass) {
			$this->items = $items;
			//$count = count($items);
			//echo "Catalog: size = $count<br />\n";
		} else {
			throw (new \InvalidArgumentException('Invalid item type, expected DisplayableItem or its subclass', 999));
		}
	}
	
	public function get_num_pages() {
		$num_items = $this->get_num_items();
		$page_size =  $this->get_page_size();
		$count = ceil($num_items / $page_size);
		//echo "Number of pages ($page_size : $num_items): $count <br />\n";
		return $count;
	}
	
	/*
	 * IPaginationDataSource interface
	 */
	
	public function get_num_items() {
		$count = count($this->items);
		//echo "Number of items: $count <br />\n";
		return $count;
	}
	
	public function get_page_size() {
		return ($this->row_size * $this->col_size);
	}
	
	public function get_item_content($it_num, $page_num) {
		return "<p>Content for item $it_num on page $page_num</p>";;
	}
	
	public function get_page_content($page_num) {
		return "<p>Content for page $page_num</p>";
	}
	
}

?>